package com.service;


import com.dao.ManageJdbc;
import com.model.Manage;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ManageService {
    ManageJdbc mj =new ManageJdbc();
    public List<Manage> show() {
        List<Manage> list = new ArrayList<Manage>();
        ResultSet rs = mj.show();
        try {
            while (rs.next()) {
                int uid = rs.getInt("uid");
                String username = rs.getString("username");
                String password = rs.getString("password");
                Manage manage = new Manage(uid, username, password);
                list.add(manage);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public void add(String username, String password) {
        mj.add( username,  password);
    }


    public void delete(String id) {
        mj.delete(id);
    }

    public Manage toupdate(String id) {
        ResultSet rs = mj.show();
        Manage manage=null;
        try {
            while (rs.next()) {
                int uid = rs.getInt("uid");

                if(id.equals(uid+"")){
                    String username = rs.getString("username");
                    String password = rs.getString("password");
                    manage = new Manage(uid, username, password);
                }
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return manage;
    }

    public void update(String uid, String username, String password) {
        mj.update(uid, username, password);
    }
}


